using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace FrameWork.TileMapManager
{
    public class MapController : MonoBehaviour
    {
        [Header("开垦荒地")]
        [SerializeField] private MapModel mapModel;
        [SerializeField] private CultivableTileView cultivableTileView;
        
        [Header("种植作物")]
        [SerializeField] private CropView cropView;

        private void Start()
        {
            mapModel.Init(new List<Vector3Int>(), new Dictionary<Vector3Int, CropData>());
            // 添加事件注册
            EventSystem.Instance.AddListener<Vector3>(EventName.EVENT_TILEMAP_CULTIVATETILE, CultivateTile);
            EventSystem.Instance.AddListener<Vector3, string>(EventName.EVENT_TILEMAP_PLANTCROP, PlantCropTile);
            EventSystem.Instance.AddListener<Vector3>(EventName.EVENT_TILEMAP_WATERCROP, WaterCrop);
            EventSystem.Instance.AddListener<Vector3>(EventName.EVENT_TILEMAP_HARVEST, HarvestCrop);
            mapModel.OnTileCultivated += cultivableTileView.UpdateView;
            mapModel.OnTileCrop += cropView.UpdateView;
            mapModel.OnTileCropClear += cropView.CelarView;
            
            mapModel.OnUpdateInfo += cropView.PlantCropTileViewInit;
            mapModel.OnUpdateInfo += cultivableTileView.CultivatableTileViewInit;
            
            StartCoroutine(Initialize());
        }

        private IEnumerator Initialize()
        {
            // 等待 0.5 秒 初始化地图
            yield return new WaitForSecondsRealtime(1.5f);
        
            // 调用 mapModel 的 Initialize 方法
            if (mapModel != null)
            {
                mapModel.Initialize();
            }
            else
            {
                Debug.LogError("mapModel 不存在!!!");
            }
        }

        private void OnDisable()
        {
            // 移除事件注册
            EventSystem.Instance.RemoveListener<Vector3>(EventName.EVENT_TILEMAP_CULTIVATETILE, CultivateTile);
            EventSystem.Instance.RemoveListener<Vector3, string>(EventName.EVENT_TILEMAP_PLANTCROP, PlantCropTile);
            EventSystem.Instance.RemoveListener<Vector3>(EventName.EVENT_TILEMAP_WATERCROP, WaterCrop);
            EventSystem.Instance.RemoveListener<Vector3>(EventName.EVENT_TILEMAP_HARVEST, HarvestCrop);
            mapModel.OnTileCultivated -= cultivableTileView.UpdateView;
            mapModel.OnTileCrop -= cropView.UpdateView;
            mapModel.OnTileCropClear -= cropView.CelarView;
            
            mapModel.OnUpdateInfo -= cropView.PlantCropTileViewInit;
            mapModel.OnUpdateInfo -= cultivableTileView.CultivatableTileViewInit;
        }

        /// <summary>
        /// 耕地
        /// </summary>
        private void CultivateTile(Vector3 mousePosition)
        {
            mapModel.CultivateTile(cultivableTileView.WorldToCell(mousePosition));
        }
        
        /// <summary>
        /// 种植
        /// </summary>
        private void PlantCropTile(Vector3 mousePosition, string itemName)
        {
            mapModel.PlantCropTile(cropView.WorldToCell(mousePosition), itemName);
        }
        
        /// <summary>
        /// 浇水
        /// </summary>
        private void WaterCrop(Vector3 mousePosition)
        {
            mapModel.WaterPlantCropTile(cropView.WorldToCell(mousePosition));
        }
        
        /// <summary>
        /// 收获
        /// </summary>
        private void HarvestCrop(Vector3 mousePosition)
        {
            mapModel.HarvestCrop(cropView.WorldToCell(mousePosition));
        }
    }
}